﻿using DataShow.codeDAL;
using DataShow.codeModel;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;

namespace DataShow.codeBLL
{
    public class ForecastBLL
    {
        #region 搜索预报
        /// <summary>
        /// 搜索预报
        /// </summary>
        /// <param name="pageSize"></param>
        /// <param name="pageIndex"></param>
        /// <param name="tableName"></param>
        /// <param name="timeBegin"></param>
        /// <param name="timeEnd"></param>
        /// <param name="elements"></param>
        /// <param name="shixiao"></param>
        /// <param name="stations"></param>
        /// <param name="sendpoints"></param>
        /// <returns></returns>
        public List<ForecastResult> Search(int pageSize, int pageIndex, string tableName, DateTime timeBegin, DateTime timeEnd, List<Element> elements, List<ForeSpanType> shixiao, List<Station> stations, List<SendPoint> sendpoints, bool isOptimize, string mode = "value")
        {
            List<ForecastResult> results = new List<ForecastResult>();
            ForecastResult item;
            DataTable dt = new DataTable();
            string values = null;
            string condition = "";
            string sendpointcondition = "''";
            if (sendpoints.Count > 0)
            {
                sendpointcondition = "";
                foreach (var type in sendpoints)
                {
                    sendpointcondition += ("'" + type + "',");
                }
                sendpointcondition = sendpointcondition.Substring(0, sendpointcondition.Length - 1);
            }
            else
            {
                sendpointcondition = "''";
            }
            string shixiaocondition = "''";
            if (shixiao.Count > 0)
            {
                shixiaocondition = "";
                foreach (var type in shixiao)
                {
                    shixiaocondition += ("'" + type + "',");
                }
                shixiaocondition = shixiaocondition.Substring(0, shixiaocondition.Length - 1);
            }
            else
            {
                shixiaocondition = "''";
            }
            string elementconditon = "''";
            if (elements.Count > 0)
            {
                elementconditon = "";
                foreach (var type in elements)
                {
                    elementconditon += ("'" + type + "',");
                }
                elementconditon = elementconditon.Substring(0, elementconditon.Length - 1);
            }
            else
            {
                elementconditon = "''";
            }
            condition = "sendpoint in (" + sendpointcondition + ") and span in (" + shixiaocondition + ") and type in(" + elementconditon + ") and CONVERT(datetime,(select substring([date],0,5)+'-'+substring([date],5,2)+'-'+substring([date],7,2))) between convert(datetime,'" + timeBegin.ToShortDateString() + "') and convert(datetime,'" + timeEnd.ToShortDateString() + "')";
            if (!isOptimize) //不是优化
            {
                dt = new codeDAL.ForecastProcess().Search(tableName, pageSize, pageIndex, condition);
            }
            else   //优化
            {
                dt = new codeDAL.ForecastProcess().Search("NEW_" + tableName, pageSize, pageIndex, condition);
            }
            if (dt.Rows.Count > 0)
            {
                foreach (DataRow row in dt.Rows)
                {
                    if (mode == "value")
                    {
                        values = row["value"].ToString();
                    }
                    else
                    {
                        values = row["semi"].ToString();
                        if (string.IsNullOrEmpty(values))
                        {
                            continue;
                        }
                    }
                    
                    string[] stationValues = values.Split('|');
                    if (stations != null)
                    {
                        foreach (var station in stations)
                        {
                            var nowstation = stationValues.Where(x => x.Contains(((long)station).ToString())).FirstOrDefault();
                            if (nowstation != null)
                            {
                                string value = nowstation.Split(';')[1];
                                item = new ForecastResult() { Date = row["date"].ToString(), City = station.ToString(), Element = DictionaryConverter.ConvertElement(row["type"].ToString()), Point = row["clock"].ToString(), SendPoint = row["sendpoint"].ToString(), Scene = row["span"].ToString(), Value = row["type"].ToString() == "wth" ? ConvertValue(value) : value };
                                results.Add(item);
                            }
                        }
                    }

                }
            }
            return results;
        }
        #endregion



        #region 降雨量代码与显示值转换
        /// <summary>
        /// 降雨量代码与显示值转换
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        private string ConvertValue(string input)
        {
            string sql = "select * from Weather_code where id=" + input;
            DataTable dt = new codeDAL.SqlHelper().ExecuteQuery(sql, CommandType.Text);
            if (dt.Rows.Count > 0)
            {
                return dt.Rows[0]["text"].ToString();
            }
            else
            {
                return input;
            }
        }
        #endregion

        #region 获取分页总数
        /// <summary>
        /// 获取分页总数
        /// </summary>
        /// <param name="tableName"></param>
        /// <param name="timeBegin"></param>
        /// <param name="timeEnd"></param>
        /// <param name="elements"></param>
        /// <param name="shixiao"></param>
        /// <param name="stations"></param>
        /// <param name="sendpoints"></param>
        /// <param name="isOptimize"></param>
        /// <returns></returns>
        public int CalculateCount(string tableName, DateTime timeBegin, DateTime timeEnd, List<Element> elements, List<ForeSpanType> shixiao, List<Station> stations, List<SendPoint> sendpoints, bool isOptimize,string mode="value")
        {
            List<ForecastResult> results = new List<ForecastResult>();
            ForecastResult item;
            DataTable dt = new DataTable();
            string values = null;
            string condition = "";
            string sendpointcondition = "''";
            if (sendpoints.Count > 0)
            {
                sendpointcondition = "";
                foreach (var type in sendpoints)
                {
                    sendpointcondition += ("'" + type + "',");
                }
                sendpointcondition = sendpointcondition.Substring(0, sendpointcondition.Length - 1);
            }
            else
            {
                sendpointcondition = "''";
            }
            string shixiaocondition = "''";
            if (shixiao.Count > 0)
            {
                shixiaocondition = "";
                foreach (var type in shixiao)
                {
                    shixiaocondition += ("'" + type + "',");
                }
                shixiaocondition = shixiaocondition.Substring(0, shixiaocondition.Length - 1);
            }
            else
            {
                shixiaocondition = "''";
            }
            string elementconditon = "''";
            if (elements.Count > 0)
            {
                elementconditon = "";
                foreach (var type in elements)
                {
                    elementconditon += ("'" + type + "',");
                }
                elementconditon = elementconditon.Substring(0, elementconditon.Length - 1);
            }
            else
            {
                elementconditon = "''";
            }
            condition = "sendpoint in (" + sendpointcondition + ") and span in (" + shixiaocondition + ") and type in(" + elementconditon + ") and CONVERT(datetime,(select substring([date],0,5)+'-'+substring([date],5,2)+'-'+substring([date],7,2))) between convert(datetime,'" + timeBegin.ToShortDateString() + "') and convert(datetime,'" + timeEnd.ToShortDateString() + "')";
            if (!isOptimize)
            {
                dt = new codeDAL.SceneProcess().CalculateCount(tableName, condition,mode);
            }
            else
            {
                dt = new codeDAL.SceneProcess().CalculateCount("NEW_" + tableName, condition,mode);
            }
            return dt.Rows.Count;
        }
        #endregion
    }
}